package com.ks.entity;

import java.io.Serializable;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ks.listener.UserSessionListener;

public class User implements HttpSessionBindingListener, Serializable {
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = LoggerFactory.getLogger(User.class);
	private String username;
	private String password;
	private int age;
	private String hobby;
	private String address;
	private String img;

	public User() {
		super();
	}

	public User(String username, String password, int age, String hobby, String address, String img) {
		super();
		this.username = username;
		this.password = password;
		this.age = age;
		this.hobby = hobby;
		this.address = address;
		this.img = img;
	}

	@Override // 同一用户再次登录会把此用户移除到在线用户列表中
	public void valueBound(HttpSessionBindingEvent event) {
		LOGGER.debug("UserSession属性绑定,用户【{}】添加到在线用户列表中！", this.getUsername());

		OnlineUser.getInstance().addUser(event.getSession().getId(), this.getUsername());
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
		LOGGER.debug("UserSession属性解除绑定,用户【{}】从在线用户列表中移除！", this.getUsername());
		// 获取同一用户上一个登录用户的session,如果不为空移除同一用户上一个会话在线，否则删除本次会话用户
		HttpSession session = UserSessionListener.sessionMap.get(this.getUsername());
		if (session != null) {
			OnlineUser.getInstance().removeUser(session.getId());
		} else {
			OnlineUser.getInstance().removeUser(event.getSession().getId());
		}
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public int getAge() {
		return age;
	}

	public String getAddress() {
		return address;
	}

	public String getImg() {
		return img;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setImg(String img) {
		this.img = img;
	}

}
